<?php
namespace App\Http\Controllers\Common;

use GuzzleHttp\Client;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

class LocationController extends Controller
{

    /**
     * 根据ip地址获取地址信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function ip(Request $request)
    {

        $ip = $request->ip();

        $return = [
            'ip' => $ip
        ];

        $client = new Client();

        if ($ip != '127.0.0.1') {
            $res = $client->get('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $ip);
        } else {
            $res = $client->get('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json');
        }
        if ($res->getStatusCode() == 200) {
            $content = \GuzzleHttp\json_decode($res->getBody()->getContents(), true);
            if (isset($content['province']) && !empty($content['province'])) {
                $return['province'] = $content['province'];
                $return['city'] = $content['city'];
            }
        }

        return response()->json($return);

    }

    /**
     * 根据经纬度获取地址信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function coordinate(Request $request)
    {

        $latitude = $request->input('latitude');
        $longitude = $request->input('longitude');
        /**
         * 坐标类型
         * 1 GPS坐标
         * 2 sogou经纬度
         * 3 baidu经纬度
         * 4 mapbar经纬度
         * 5 腾讯、google、高德坐标
         * 6 sogou墨卡托
         */
        $coord_type = $request->input('coord_type');

        if (empty($latitude)) {
            return response()->json(['code' => 10001, 'message' => '缺少参数：latitude']);
        }
        if (empty($longitude)) {
            return response()->json(['code' => 10002, 'message' => '缺少参数：longitude']);
        }
        if (!in_array($coord_type, [1, 2, 3, 4, 5, 6])) {
            $coord_type = 5;
        }

        $client = new Client();

        $http_data = [
            'location' => $latitude . ',' . $longitude,
            'coord_type' => $coord_type,
            'key' => 'G54BZ-CXDKX-OMO4R-ZD6MU-POXZO-YDFL3',
            'output' => 'json'
        ];
        $res = $client->get('http://apis.map.qq.com/ws/geocoder/v1/',  ['query' => $http_data]);
        if ($res->getStatusCode() != 200) {
            return response()->json(['code' => 10003, 'message' => '网络请求失败']);
        }
        $location = json_decode($res->getBody()->getContents(),true);
        if ($location['status'] != 0) {
            return response()->json(['code' => 10004, 'message' => $location['message']]);
        }

        $return_data = [
            'address' => $location['result']['address']
        ];
        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $return_data]);

    }

    /**
     * 地址解析
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function address(Request $request)
    {

        $address = $request->input('address');
        if (empty($address)) {
            return response()->json(['code' => 10001, 'message' => '缺少参数：address']);
        }

        $client = new Client();

        $http_data = [
            'address' => $address,
            'key' => 'G54BZ-CXDKX-OMO4R-ZD6MU-POXZO-YDFL3',
            'output' => 'json'
        ];
        $res = $client->get('http://apis.map.qq.com/ws/geocoder/v1/',  ['query' => $http_data]);
        if ($res->getStatusCode() != 200) {
            return response()->json(['code' => 10003, 'message' => '网络请求失败']);
        }
        $location = json_decode($res->getBody()->getContents(),true);
        if ($location['status'] != 0) {
            return response()->json(['code' => 10004, 'message' => $location['message']]);
        }

        $return_data = [
            'latitude' => $location['result']['location']['lat'],
            'longitude' => $location['result']['location']['lng'],
        ];
        return response()->json(['code' => 200, 'message' => 'ok', 'data' => $return_data]);

    }

}